package com.mcxx.modules.system.controller;

import com.google.common.base.Strings;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.system.service.SalvationStandardService;
import com.mcxx.modules.miniSalvation.order.entity.StandardEntity;
import com.mcxx.modules.miniSalvation.order.service.read.StandardHistoryReadService;
import com.mcxx.modules.miniSalvation.order.service.read.StandardReadService;
import com.mcxx.modules.miniSalvation.order.service.write.StandardWriteService;
import com.mcxx.modules.miniSalvation.welfare.dto.WelfareDTO;
import com.mcxx.modules.miniSalvation.welfare.param.WelfareParam;
import com.mcxx.modules.system.dto.StandardDTO;
import com.mcxx.modules.system.param.StandardParam;
import com.mcxx.modules.system.param.WelfareCheckParam;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;



@Api("救助标准配置,调整标准/公式等")
@RestService("/dataManagement/salvationStandard/v2")
public class SalvationStandardController extends DefaultRestService {
    private static final Logger logger = LoggerFactory.getLogger(SalvationStandardController.class);
    @Autowired
    private StandardWriteService standardWriteService;
    @Autowired
    private SalvationStandardService salvationStandardService;
    @Autowired
    private StandardReadService standardReadService;
    @Autowired
    private StandardHistoryReadService standardHistoryReadService;





    @RequestMapping(value = "querySalvationStandardList",method = RequestMethod.POST)
    @ApiOperation(value="根据条件查询救助标准列表")
    public void querySalvationStandardList(StandardParam param, PageParam pageParam){
        UserAccountVo vo = getCurUserAccount();
//        param.setOrgId(getCurUserAccount().getOrgId());
        param.setAreaCode(getCurUserAccount().getAreaCode());
        Page page = standardReadService.getSalvationStandardList(param, pageParam);
        this.setPageBean(page);
    }


    @RequestMapping(value = "saveSalvationStandard",method = RequestMethod.POST)
    @ApiOperation(value="保存救助标准记录")
    public void saveSalvationStandard(StandardEntity standardEntity){
        standardEntity.setOrgId(getCurUserAccount().getOrgId());
        standardEntity.setAreaCode(getCurUserAccount().getAreaCode());
        standardWriteService.addStandard(standardEntity);
    }


    @RequestMapping(value = "modifySalvationStandard",method = RequestMethod.POST)
    @ApiOperation(value="修改救助标准记录")
    public void modifySalvationStandard(StandardEntity standardEntity){
        standardWriteService.updateIgnoreNull(standardEntity);
        setData(true);
    }


    @RequestMapping(value = "modifySalvationStandardStatus",method = RequestMethod.POST)
    @ApiOperation(value="修改救助标准记录状态（1 启用，2停用(注销)，3 冻结,4 未启用）")
    public void modifySalvationStandardStatus(@V({"notnull"}) String id, @V({"notnull"})String status, String gearCode,String businessType){
        logger.info("修改救助标准记录状态,参数：id:{}, status:{}，（1 启用，2停用(注销)，3 冻结,4 未启用）", id, status);
        standardWriteService.updateSalvationStandardStatus(id, status, gearCode, getCurUserAccount().getAreaCode(), getCurUserAccount().getOrgId(),businessType);
        logger.info("修改救助标准记录状态,结果:{}", true);
    }


    @RequestMapping(value = "delSalvationStandard",method = RequestMethod.POST)
    @ApiOperation(value="删除救助标准记录")
    public void delSalvationStandard(@V({"notnull"}) String id, String gearCode) throws BizException{
        logger.info("删除救助标准记录,参数：{}", id);
        int i = standardReadService.getOrderByGear("2", gearCode, getCurUserAccount().getAreaCode());
        if(i <= 0){
            standardWriteService.delSalvationStandard(id);
        }else{
            setError("当前救助业务在保人数为:"+i+"，不能删除");
        }
        logger.info("删除救助标准记录,结果：{}", true);
    }



    @RequestMapping(value = "delSalvationStandards",method = RequestMethod.POST)
    @ApiOperation(value="删除救助标准记录")
    public void delSalvationStandards(@V({"notnull"}) String id, String gearCode) throws BizException{
        logger.info("删除救助标准记录,参数：{}", id);
        standardWriteService.delSalvationStandard(id);
        logger.info("删除救助标准记录,结果：{}", true);
    }


    @RequestMapping(value = "getSalvationDetail",method = RequestMethod.POST)
    @ApiOperation(value="查询对应的救助标准详情")
    public void getSalvationDetail(String pId){
        logger.info("查询对应的救助标准详情,参数：{}", pId);
        Map<String, Object> standardDetailMap = salvationStandardService.getSalvationDetail(pId);
        setData("data", standardDetailMap);
        logger.info("查询对应的救助标准详情,结果：{}", standardDetailMap);
    }






    @RequestMapping(value = "getOrderByGear",method = RequestMethod.POST)
    @ApiOperation(value="根据档位查询在保人数")
    public void getOrderByGear(@V("notnull") String gearCode){
        int i = standardReadService.getOrderByGear("1", gearCode, getCurUserAccount().getAreaCode());
        setData("data", i);
    }


    @RequestMapping(value = "saveSalvationDw",method = RequestMethod.POST)
    @ApiOperation(value="保存救助标准档位")
    public void saveStandardDiBao(@RequestBody StandardDTO standardDto){
        logger.info("保存救助标准调整,参数：{}", standardDto);
        standardDto.setStandType(standardDto.getType());
        salvationStandardService.updateStandard(standardDto, getCurUserAccount());
        setData("data", true);
        logger.info("保存救助标准调整结束，结果：true");
    }


    @RequestMapping(value = "tryStandardChange",method = RequestMethod.POST)
    @ApiOperation(value="救助标准调整试算")
    public void tryStandardChange(@RequestBody StandardDTO standardDto){
        logger.info("救助标准调整试算,参数：{}", standardDto);
        standardDto.setStandType(standardDto.getType());
        String retMsg = salvationStandardService.updateStandard(standardDto, getCurUserAccount());
        setData("data", retMsg);
        logger.info("救助标准调整试算结束，结果：true");
    }


    @RequestMapping(value = "confirmWelfareResult",method = RequestMethod.POST)
    @ApiOperation(value="确认调标结果")
    public void confirmWelfareResult(String id){
        logger.info("确认调标结果,参数：{}", id);
        String retMsg = salvationStandardService.confirmWelfareResult(id, getCurUserAccount());
        setData("data", retMsg);
        logger.info("确认调标结果完成！");
    }


    @RequestMapping(value = "cancelWelfare",method = RequestMethod.POST)
    @ApiOperation(value="撤销调标记录")
    public void cancelWelfare(String id){
        logger.info("撤销调标记录,standardId={}", id);
        String retMsg = salvationStandardService.cancelWelfare(id);
        setData("data", retMsg);
        logger.info("撤销调标完成");
    }


    @RequestMapping(value = "diBaoGearTrial",method = RequestMethod.POST)
    @ApiOperation(value="低保档位调整试算")
    public void diBaoGearTrial(String businessType, String standType){

    }





    @RequestMapping(value = "getStandardHistoryList",method = RequestMethod.POST)
    @ApiOperation(value="根据条件查询救助标准列表")
    public void getStandardHistoryList(StandardParam param, PageParam pageParam){
        param.setOrgId(getCurUserAccount().getOrgId());
        param.setAreaCode(getCurUserAccount().getAreaCode());
        Page page = standardHistoryReadService.getStandardHistoryList(param, pageParam);
        this.setPageBean(page);
    }


    @RequestMapping(value = "getStandardHisById",method = RequestMethod.POST)
    @ApiOperation(value="根据标准ID获取标准历史记录")
    public void getStandardHisById(StandardParam param){
        String standardHisId = salvationStandardService.getStandardHisByStandardId(param.getStandardId());
        this.setData("data", standardHisId);
    }


    @RequestMapping(value = "getSalvationHisDetail",method = RequestMethod.POST)
    @ApiOperation(value="查询对应的救助标准详情")
    public void getSalvationHisDetail(String pId){
        logger.info("查询对应的救助标准详情,参数：{}", pId);
        Map<String, Object> standardDetailMap = standardHistoryReadService.getSalvationDetail(pId);
        setData("data", standardDetailMap);
        logger.info("查询对应的救助标准详情,结果：{}", standardDetailMap);
    }


    @RequestMapping(value = "queryWelFamilyCount",method = RequestMethod.POST)
    @ApiOperation(value="查询对应的救助标准详情")
    public void queryWelFamilyCount(String histId){
        logger.info("查询对应的救助标准详情,参数：{}", histId);
        WelfareDTO welfareDTO = new WelfareDTO();
        setData("data", welfareDTO);
        logger.info("查询对应的救助标准详情,结果：{}", welfareDTO);
    }


    @RequestMapping(value = "queryWelFamilyList1",method = RequestMethod.POST)
    @ApiOperation(value="根据条件查询救助标准列表")
    public void queryWelFamilyList(WelfareParam param, PageParam pageParam){
        this.setPageBean(null);
    }


    @PostMapping({"downLoadWelfareTempData"})
    public void downLoadWelfareTempData(HttpServletResponse response, @RequestBody WelfareParam param) throws IOException {
        logger.info("下载调标试算结果：{}", param);
        ExcelInfo excelInfo = salvationStandardService.exportExcelWelfareTemp(param, getCurUserAccount());
        if(null != excelInfo){
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @RequestMapping(value = "listOfWelfareCheck",method = RequestMethod.POST)
    @ApiOperation(value="调标数据验证列表")
    public void listOfWelfareCheck(WelfareCheckParam param, PageParam pageParam){
        logger.info("开始查询调标验证数据，参数是：{}", param);
        UserAccountVo vo = getCurUserAccount();
        this.setPageBean(null);
    }


    @PostMapping("downLoadWelfareCheck")
    @ApiOperation(value="导出调标前差异性数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出差异性数据信息")
    public void downLoadWelfareCheck(HttpServletResponse response, @RequestBody WelfareCheckParam param)throws IOException {
        logger.info("开始导出调标前差异性数据，参数是：{}", param);
    }


    @RequestMapping(value = "analysisErrorData",method = RequestMethod.POST)
    @ApiOperation(value="分析差异性数据")
    public void analysisErrorData(StandardDTO standardDto){
        logger.info("分析差异性数据,参数：{}", standardDto);
    }
}
